// pages/detail/detail.js
import {Goods} from "../../model/goods";
import WxParse from "../../lib/wxParse/wxParse";
import {getWindowHeightRpx} from "../../utils/system";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: 0,
        goodsInfo: {},
        attributes: [],
        specificationList: [],
        showSpecs: false,
        productList: [],
        collectImage: '/static/images/icon_like.png',
        noCollectImage: '/static/images/icon_like.png',
        hasCollectImage: '/static/images/icon_like_checked.png',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {    
        const pid = options.pid
        const windowHeight = await getWindowHeightRpx()
        const h = windowHeight - 100
        this.setData({
            h
        });
        this.data.goods = new Goods()
        await this.initGoodsInfo(pid)
    },

    async initGoodsInfo(id) {
        const detail = await this.data.goods.getDetail(id)
        if(detail.goods && detail.goods.galleryList && detail.goods.galleryList.length > 0) {
            const processedGalleryList = detail.goods.galleryList.map(this.processSwiperImage)
        }
        let _specificationList = detail.specifications
        if(_specificationList.length === 1) {
            if(_specificationList[0].valueList.length == 1) {
                _specificationList[0].valueList[0].checked = true

                this.setData({
                    checkedSpecText: _specificationList[0].valueList[0].value,
                    tmpSpecText: '已选择：' + _specificationList[0].valueList[0].value
                })
            }
        }
        this.setData({
            goodsInfo: detail.goods,
            attributes: detail.attributes,
            specificationList: detail.specifications,
            productList: detail.products
        })
        if(detail.userHasCollect) {
            this.setData({
                collectImage: this.data.hasCollectImage
            });
        } else {
            this.setData({
                collectImage: this.data.noCollectImage
            });
        }

        WxParse.wxParse('goodsDetail', 'html', detail.goods.detailText, this);
    },

    processSwiperImage(img) {
        if (img) {
            // 直接返回修改后的图片URL
            return `${img}?imageView2/0/w/400`;
          }
    },

    async handleCollect() {
        const res = await this.data.goods.collect(this.data.id)
        if(res) {
            this.setData({
                collectImage: this.data.hasCollectImage
            });
        }else {
            this.setData({
                collectImage: this.data.noCollectImage
            });
        }
    },

    switchSpecPop() {
      if(!this.data.showSpecs){
          this.setData({
              showSpecs: true
          })
      }
    },
    closeSpecs() {
        this.setData({
            showSpecs: false
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        let that = this;
        let userInfo = wx.getStorageSync('userInfo');
        let shareUserId = 1;
        if (userInfo) {
            shareUserId = userInfo.userId;
        }
        console.log('分享地址：' + '/pages/detail/detail?pid=' + this.data.id + '&shareUserId=' + shareUserId);
        return {
            title: that.data.goodsInfo.name,
            desc: '好货分享',
            path: '/pages/detail/detail?pid=' + this.data.id + '&shareUserId=' + shareUserId
        }
    }
})